*&---------------------------------------------------------------------*
*& Report ZEX_LISTING_59                                               *
*&---------------------------------------------------------------------*
*& Created By: James Wood (james.wood@bowdarkconsulting.com)           *
*& Created On: 12/12/2008                                              *
*& Purpose:    This program demonstrates how to remove persistent      *
*&             objects using the ABAP Persistence Service.             *
*&---------------------------------------------------------------------*
REPORT zex_listing_114.

*----------------------------------------------------------------------*
* START-OF-SELECTION Event Module                                      *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM remove_person.

*&---------------------------------------------------------------------*
*&      Form  remove_person
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM remove_person.

* Local Data Declarations:
  DATA: lr_agent     TYPE REF TO zca_os_person,
        lt_people    TYPE osreftab,
        lr_oref      TYPE REF TO object,
        lr_query_mgr TYPE REF TO if_os_query_manager,
        lr_query     TYPE REF TO if_os_query,
        lr_person    TYPE REF TO zcl_os_person.

  TRY.
*   Create a new query based on the first name attribute of
*   the "Person" persistent object:
    lr_agent = zca_os_person=>agent.
    lr_query_mgr = cl_os_system=>get_query_manager( ).
    lr_query = lr_query_mgr->create_query(
      i_filter = 'NAME_FIRST = PAR1' ).

*   Lookup a particular person entity using a query:
    lt_people =
      lr_agent->if_os_ca_persistency~get_persistent_by_query(
        i_query = lr_query
        i_par1 = 'Andersen' ).

    READ TABLE lt_people INTO lr_oref INDEX 1.
    IF sy-subrc EQ 0.
*     Delete the person object:
      lr_person ?= lr_oref.
      lr_agent->if_os_factory~delete_persistent( lr_person ).
      COMMIT WORK.
    ENDIF.
  CATCH cx_os_object_not_found.

  CATCH cx_os_query_error.

  ENDTRY.

ENDFORM.                    "remove_person